package com.mostka.ws.client;

import com.google.gwt.core.client.GWT;



public class WebSocket{
	
	public static int CONNECTING = 0;
	public static int OPEN = 1;
	public static int CLOSING = 2;
	public static int CLOSED = 3;
	
	private JsWebSocket ws;
	
	public WebSocket(String channel) {
		this(GWT.getHostPageBaseURL().replace("http", "ws") + "socket",channel);
	}
	
	public WebSocket(String location, String channel) {
		ws = JsWebSocket.create(location+"/"+channel);
	}
	
	public void setHandler(ConnectionHandler handler){
		ws.onOpen(handler);
		ws.onClose(handler);
		ws.onError(handler);
		ws.onMessage(handler);
	}
	
	public void close(){
		ws.close();
	}
	
	public void send(String message){
		ws.send(message);
	}
	
	public static boolean isSupported(){
		return JsWebSocket.isSuported();
	}
	
}
